package model;

import java.sql.*;
import java.util.Vector;


public class IconDAO {
	
	//아이콘 등록
	public boolean insert(Icon icon) {
		Connection conn = ConnectionManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into icon(iconname, iconfile) values(?,?)");			
			ps.setString(1, icon.getIconname());
			ps.setString(2, icon.getIconfile());		

			ps.executeUpdate();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//아이콘 삭제
	public boolean delete(Icon icon) {
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			st.executeUpdate("delete from icon where iconno=" + icon.getIconno());

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//아이콘 수정
	public boolean updateIcon(Icon icon) {
		Connection conn = ConnectionManager.getConnection();				
		try {			
			PreparedStatement ps = conn.prepareStatement("update icon set iconname=? where iconno="+icon.getIconno());
			ps.setString(1, icon.getIconname());					
			ps.executeUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}	
	
	//전체 아이콘 수
	public int getTotalRows() {
		int totalRows = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select count(*) from icon");
			rs.next();
			totalRows = rs.getInt(1);
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return totalRows;
	}

	//아이콘 보기 
	public Icon getIcon(int iconno) {
		Connection conn = ConnectionManager.getConnection();
		Icon icon = null;
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st
					.executeQuery("select * from icon where iconno=" + iconno);
			if (rs.next()) {
				icon = new Icon();
				icon.setIconno(rs.getInt(1));
				icon.setIconname(rs.getString(2));
				icon.setIconfile(rs.getString(3));
			}
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return icon;
	}

	//전체 아이콘 개수
	public Vector getList() {
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from icon");
			while (rs.next()) {
				Icon icon = new Icon();
				icon.setIconno(rs.getInt(1));
				icon.setIconname(rs.getString(2));
				icon.setIconfile(rs.getString(3));
				list.add(icon);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return list;
	}

}
